﻿using SLAssetSystem;
using SLTimeSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SLBehaviourSystem
{
    public delegate void BehaviourTreeCompleted();
    public class BehaviourTree : BehaviourNodeBase
    {
        private int excuteCount = 0;
        public Dictionary<int, BehaviourNodeBase> NodeDict = new Dictionary<int, BehaviourNodeBase>();
        public BehaviourTreeJsonData Tree;
        public event BehaviourTreeCompleted OnBehaviourTreeCompleted;

        private TimeAgent timerAgent;

        public bool IsAutoExcute;
        public BehaviourTree()
        {
            Id = -1;
            IsAutoExcute = true;
        }

        protected override void OnInitData()
        {
            
        }

        protected override void OnExcute()
        {
            DebugUtils.Log("BehaviourTree OnExcute" + Tree.TreeId);
            curNodeState = childNodes[0].Excute();
        }

        protected override void OnChildNodeComplete()
        {

            if (childNodes[0].CurNodeState == BehaviourNodeState.Success || childNodes[0].CurNodeState == BehaviourNodeState.Failure)
            {
                //if (GameDefines.Mode==ModeType.Debug)
                //{
                //    excuteCount++;
                //    if (excuteCount>10)
                //    {
                //        DebugUtils.Pause();
                //    }
                //}
                
                Reset();
                if (IsAutoExcute)
                {
                    Excute();
                }
            }
        }

        protected override void OnInterupt()
        {

        }

        protected override void OnReset()
        {
            if (OnBehaviourTreeCompleted != null)
            {
                OnBehaviourTreeCompleted.Invoke();
            }
        }
    }
}
